<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['title', validatorRules.title]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['source']" placeholder="请输入来源"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="一级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['firstType', validatorRules.firstType]" :trigger-change="true" dictCode="article_type" placeholder="请选择一级分类"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="二级分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['secondType', validatorRules.secondType]" :trigger-change="true" dictCode="notice" placeholder="请选择二级分类"/>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="内容" :labelCol="{span:2}" :wrapperCol="{span:22}">
              <!--              <j-editor v-decorator="['content',{trigger:'input'}]"/>-->
              <div v-html="model.content"></div>
            </a-form-item>
          </a-col>
          <!--<a-col :span="24">
            <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isEnabled']" :trigger-change="true" dictCode="valid_status" placeholder="请选择是否启用"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否顶置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isTop']" :trigger-change="true" dictCode="is_top" placeholder="请选择是否顶置"/>
            </a-form-item>
          </a-col>-->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <a-form>
      <a-row>
        <a-col :span="24">
          <a-form-item label="审核意见" :labelCol="{span:2}" :wrapperCol="{span:21}">
            <a-textarea v-model="remark" placeholder="请输入审核意见" :rows="4"></a-textarea>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col  :span="24" style="text-align: center">
          <a-button @click="passForm" style="margin-right: 10px;" type="primary">通过</a-button>
          <a-button @click="nopassForm" type="danger" >驳回</a-button>
          <!--          <a-button @click="nopassForm" type="danger" v-has="'audit'">驳回</a-button>-->
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script>

  import { httpAction, getAction,postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    name: 'ShenheForm',
    components: {
      JFormContainer,
      JDictSelectTag,
      JEditor,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          title: {
            rules: [
              { required: true, message: '请输入标题!'},
            ]
          },
          content: {
            rules: [
              { required: true, message: '请输入内容!'},
            ]
          },
          firstType: {
            rules: [
              { required: true, message: '请输入一级分类，公告、新闻、政策法规!'},
            ]
          },
          secondType: {
            rules: [
              { required: true, message: '请输入二级分类，挂牌公告、成立公告、兑付公告、新闻、国家政策法规、交易规则、自律声明!'},
            ]
          },
        },
        url: {
          add: "/platformarticle/bizPlatformArticle/add",
          edit: "/platformarticle/bizPlatformArticle/edit",
          queryById: "/platformarticle/bizPlatformArticle/queryById",
          audit:"/platformarticle/bizPlatformArticle/audit"
        },
        remark:''
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'title','firstType','secondType','source'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'title','firstType','secondType','source'))
      },
      //通过
      passForm(){
        this.confirmLoading = true;
        postAction(this.url.audit,{"auditResult": 1,
          "remark": this.remark,
          "subjectCategory": "3",
          "subjectId": this.model.id}).then((res)=>{
          console.log(res)
          if (res.success){
            this.$message.success(res.message)
          }else {
            this.$message.error(res.message)
          }
        }).finally(()=>{
          this.remark = ''
          this.confirmLoading = false;
          this.$emit('ok');
        })
      },
      //拒绝
      nopassForm(){
        if (this.remark==''){
          this.$message.error('请填写审核意见！')
        }else {
          this.confirmLoading = true;
          postAction(this.url.audit,{"auditResult": 0,
            "remark": this.remark,
            "subjectCategory": "3",
            "subjectId": this.model.id}).then((res)=>{

            console.log(res)
            if (res.success){
              this.$message.success(res.message)
            }else {
              this.$message.error(res.message)
            }
          }).finally(()=>{
            this.remark = ''
            this.confirmLoading = false;
            this.$emit('ok');
          })
        }
      }
    }
  }
</script>
<style scoped>
  .ant-btn-danger{
    background-color: #ff4d4f;
  }
</style>